package com.gack.business.dao;

import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Repository;

import com.gack.business.model.StoreSupportServiceIcon;

/**
 * 
* @ClassName: StoreSupportServiceIconDao 
* @Description: TODO(这里用一句话描述这个类的作用) 
* @author (ZhangXinYu)  
* @date 2018年9月7日 上午11:27:06 
* @version V1.0
 */
@Repository
public class StoreSupportServiceIconDao {
	
	@Autowired
	private EntityManager entityManager;
	
	/**
	 * 判断名称是否重复  若传入id,则排除自身id名称 
	 */
	public boolean hasName(String id,String name) {
		boolean flag = false;
		String jpql = "select case when(count(id) > 0) then 'true' else 'false' end as hasName from store_support_service_icon where 1=1 and is_delete = 0"
					+ " and name = :name";
		if(id != null && !id.equals("")) {
			jpql += " and id <> :id";
		}
		Query query = entityManager.createNativeQuery(jpql);
		query.setParameter("name", name);
		if(id != null && !id.equals("")) {
			query.setParameter("id", id);
		}
		flag = Boolean.valueOf(query.getSingleResult().toString());
		return flag;
	}
	
	/**
	 * icon是否正在使用中  
	 */
	public boolean hasUsing(String id){
		boolean flag = false;
		String jpql = "select case when(count(*) > 0) then 'true' else 'false' end as hasUsing from store_support_service"
				+ " where 1=1 and (is_use = 0 or is_use = 1)"
				+ " and use_template = 1"
				+ " and icon = :id";
		Query query = entityManager.createNativeQuery(jpql);
		query.setParameter("id", id);
		flag = Boolean.valueOf(query.getSingleResult().toString());
		return flag;
	}
	
	/**
	 * 批量删除icon  >> 修改icon的delete状态为1
	 */
	@Modifying
	@Transactional
	public int delete(List<String> ids) {
		String jpql = "update store_support_service_icon set is_delete = 1 where id in :ids";
		Query query = entityManager.createNativeQuery(jpql);
		query.setParameter("ids", ids);
		int count = query.executeUpdate();
		return count;
	}
	
	/**
	 * 查询icon
	 */
	@SuppressWarnings("unchecked")
	public List<StoreSupportServiceIcon> search(int searchType, String keyWord, String startTime, String endTime,
			int page, int size) {
		List<StoreSupportServiceIcon> icons = null;
		String jpql = "select * from store_support_service_icon where 1=1 and is_delete = 0";
		if(searchType == 1) {
			jpql += " and name like '"+keyWord+"'";
		}else if(searchType == 2) {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
			if(startTime != null && !startTime.equals("")){
				try {
					Date d = format.parse(startTime.replace("Z", " UTC"));
					format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
					startTime = format.format(d);
				} catch (ParseException e) {
					e.printStackTrace();
				}//注意是空格+UTC
				jpql +=" and update_time >= '"+startTime+"'";
			}else if(endTime != null && !endTime.equals("")){
				try {
					Date d = format.parse(endTime.replace("Z", " UTC"));
					format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
					endTime = format.format(d);
				} catch (ParseException e) {
					e.printStackTrace();
				}//注意是空格+UTC
				jpql +=" and update_time <= '"+endTime+"'";
			}
		}
		jpql += " order by update_time desc";
		Query query = entityManager.createNativeQuery(jpql, StoreSupportServiceIcon.class);
		icons = query.setFirstResult(page * size).setMaxResults(size).getResultList();
		return icons;
	}
	
	/**
	 * 查询icon total
	 */
	public Long searchTotal(int searchType, String keyWord, String startTime, String endTime) {
		String jpql = "select count(*) from store_support_service_icon where 1=1 and is_delete = 0";
		if(searchType == 1) {
			jpql += " and name like '"+keyWord+"'";
		}else if(searchType == 2) {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
			if(startTime != null && !startTime.equals("")){
				try {
					Date d = format.parse(startTime.replace("Z", " UTC"));
					format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
					startTime = format.format(d);
				} catch (ParseException e) {
					e.printStackTrace();
				}//注意是空格+UTC
				jpql +=" and update_time >= '"+startTime+"'";
			}else if(endTime != null && !endTime.equals("")){
				try {
					Date d = format.parse(endTime.replace("Z", " UTC"));
					format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
					endTime = format.format(d);
				} catch (ParseException e) {
					e.printStackTrace();
				}//注意是空格+UTC
				jpql +=" and update_time <= '"+endTime+"'";
			}
		}
		Query query = entityManager.createNativeQuery(jpql);
		BigInteger total = (BigInteger)query.getSingleResult();
		return total.longValue();
	}
	
	/**
	 * 查询全部未删除icon
	 */
	@SuppressWarnings("unchecked")
	public List<StoreSupportServiceIcon> allIcons(){
		List<StoreSupportServiceIcon> icons = null;
		String jpql = "select * from store_support_service_icon where is_delete = 0 order by update_time desc";
		Query query = entityManager.createNativeQuery(jpql,StoreSupportServiceIcon.class);
		icons = query.getResultList();
		return icons;
	}
	
	/**
	 * 已存在的icon名称
	 */
	@SuppressWarnings("unchecked")
	public List<String> existsServiceIconNames(List<String> names){
		List<String> existsServiceIconNames = null;
		String jpql = "select name from store_support_service_icon where is_delete = 0 and name in :names";
		Query query = entityManager.createNativeQuery(jpql);
		query.setParameter("names", names);
		existsServiceIconNames = query.getResultList();
		return existsServiceIconNames;
	}
}
